package com.ruoyi.project.common.utils;

/**
 * @author sunsahnwei
 * @date 2023年03月31日 14:06
 * @fileName PrintUtil
 */

import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocFlavor.INPUT_STREAM;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;

/**
 * 打印
 *
 * @author
 * @Date 2019-07-31
 */
public class PrintUtil {
    public static void main(String[] args) {
        File file = new File("D:\\ruoyi\\uploadPath\\myword.pdf");
//	    String printerName = "D203 (HP LaserJet M1536dnf MFP)";//打印机名包含字串
        String printerName = "SF-L540H";//打印机名包含字串
        drawImage("D:\\ruoyi\\uploadPath\\myword.pdf", 210, true);
    }

    /**
     * 选择默认打印机进行打印
     * @param fileName
     * @param h
     * @param aleartPrint
     */
    public static void drawImage(String fileName, int h, boolean aleartPrint) {
        if ((fileName == null) || (fileName.trim() == "")) {
            throw new RuntimeException("文件名为空");
        }
        try {
            DocFlavor dof = null;
            if (fileName.endsWith(".gif")) {
                dof = DocFlavor.INPUT_STREAM.GIF;
            } else if (fileName.endsWith(".jpg")) {
                dof = DocFlavor.INPUT_STREAM.JPEG;
            } else if (fileName.endsWith(".png")) {
                dof = DocFlavor.INPUT_STREAM.PNG;
            }else if(fileName.endsWith(".pdf")){
                dof= INPUT_STREAM.PDF;
            }
            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            pras.add(OrientationRequested.PORTRAIT);
            pras.add(new Copies(1));
            DocAttributeSet das = new HashDocAttributeSet();
            das.add(new MediaPrintableArea(0, 0, 100, h, 1000));
            FileInputStream fin = new FileInputStream(fileName);
            Doc doc = new SimpleDoc(fin, dof, das);
            print( aleartPrint, dof, pras, doc);
            fin.close();
        } catch (IOException ie) {
            ie.printStackTrace();
        } catch (PrintException pe) {
            pe.printStackTrace();
        }
    }

    /**
     * 使用默认打印机打印
     * @param aleartPrint 是否弹出打印框
     * @param dof
     * @param pras
     * @param doc  文件
     * @throws PrintException
     */
    private static void print( boolean aleartPrint, DocFlavor dof, PrintRequestAttributeSet pras, Doc doc) throws PrintException {
        PrintService service = PrintServiceLookup.lookupDefaultPrintService();//默认打印机
        if (aleartPrint) {
            PrintService[] printServices = PrintServiceLookup.lookupPrintServices(dof, pras);
            service = ServiceUI.printDialog(null, 400, 400, printServices, service, dof, pras);
        }
        if (service != null) {
            DocPrintJob job = service.createPrintJob();
            job.addPrintJobListener(new PrintJobListener() {
                public void printJobRequiresAttention(PrintJobEvent arg0) {
                    System.out.println("printJobRequiresAttention");
                }

                public void printJobNoMoreEvents(PrintJobEvent arg0) {
                    System.out.println("打印机已接收");
                }

                public void printJobFailed(PrintJobEvent arg0) {
                    System.out.println("打印机无法完成作业,必须重新提交");
                }

                public void printJobCompleted(PrintJobEvent arg0) {
                    System.out.println("打印结束");
                }

                public void printJobCanceled(PrintJobEvent arg0) {
                    System.out.println("作业已被用户或者程序取消");
                }

                public void printDataTransferCompleted(PrintJobEvent arg0) {
                    System.out.println("数据已成功传输打印机");
                }
            });
            try {
                job.print(doc, pras);
            } catch (PrintException pe) {
                pe.printStackTrace();
            }
        } else {
            if (aleartPrint) {
                throw new RuntimeException("打印机未连接,请选择打印机");
            }
            throw new RuntimeException("请设置默认打印机");
        }
    }

    /**
     * 使用带打印机的名称进行打印
     * @param dof
     * @param pras
     * @param doc  文件
     * @throws PrintException
     */
    private static void printByName(String printerName, boolean aleartPrint, DocFlavor dof, PrintRequestAttributeSet pras, Doc doc) throws PrintException {

        //定位打印服务
        PrintService service = null;
        if (printerName != null) {
             //获得本台电脑连接的所有打印机
            PrintService[] printServices = PrinterJob.lookupPrintServices();
            if (printServices == null || printServices.length == 0) {
                System.out.print("打印失败，未找到可用打印机，请检查。");
                return;
            }
              //匹配指定打印机
            for (int i = 0; i < printServices.length; i++) {
                if (printServices[i].getName().contains(printerName)) {
                    service = printServices[i];
                    break;
                }
            }
            if (service == null) {
                System.out.print("打印失败，未找到名称为" + printerName + "的打印机，请检查。");
                return;
            }
        }
        if (aleartPrint) {
            PrintService[] printServices = PrintServiceLookup.lookupPrintServices(dof, pras);
            service = ServiceUI.printDialog(null, 400, 400, printServices, service, dof, pras);
        }
        if (service != null) {
            DocPrintJob job = service.createPrintJob();
            job.addPrintJobListener(new PrintJobListener() {
                public void printJobRequiresAttention(PrintJobEvent arg0) {
                    System.out.println("printJobRequiresAttention");
                }

                public void printJobNoMoreEvents(PrintJobEvent arg0) {
                    System.out.println("打印机已接收");
                }

                public void printJobFailed(PrintJobEvent arg0) {
                    System.out.println("打印机无法完成作业,必须重新提交");
                }

                public void printJobCompleted(PrintJobEvent arg0) {
                    System.out.println("打印结束");
                }

                public void printJobCanceled(PrintJobEvent arg0) {
                    System.out.println("作业已被用户或者程序取消");
                }

                public void printDataTransferCompleted(PrintJobEvent arg0) {
                    System.out.println("数据已成功传输打印机");
                }
            });
            try {
                job.print(doc, pras);
            } catch (PrintException pe) {
                pe.printStackTrace();
            }
        } else {
            if (aleartPrint) {
                throw new RuntimeException("打印机未连接,请选择打印机");
            }
            throw new RuntimeException("请设置默认打印机");
        }
    }
}
